{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Save/load street network models to/from disk\n",
    "\n",
    "Author: [Geoff Boeing](https://geoffboeing.com/)\n",
    "\n",
    "  - [Overview of OSMnx](http://geoffboeing.com/2016/11/osmnx-python-street-networks/)\n",
    "  - [GitHub repo](https://github.com/gboeing/osmnx)\n",
    "  - [Examples, demos, tutorials](https://github.com/gboeing/osmnx-examples)\n",
    "  - [Documentation](https://osmnx.readthedocs.io/en/stable/)\n",
    "  - [Journal article/citation](http://geoffboeing.com/publications/osmnx-complex-street-networks/)\n",
    "  \n",
    "This notebook demonstrates how to save networks to disk as shapefiles, geopackages, graphml, and xml, and how to load an OSMnx-created network from a graphml file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.16.0'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import osmnx as ox\n",
    "%matplotlib inline\n",
    "ox.config(log_console=True, use_cache=True)\n",
    "ox.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get a network\n",
    "place = 'Piedmont, California, USA'\n",
    "G = ox.graph_from_place(place, network_type='drive')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Shapefiles and GeoPackages for GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save graph as a shapefile\n",
    "ox.save_graph_shapefile(G, filepath='./data/piedmont')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save graph as a geopackage\n",
    "ox.save_graph_geopackage(G, filepath='./data/piedmont.gpkg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GraphML files for saving network and preserving topological detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save/load graph as a graphml file: this is the best way to save your model\n",
    "# for subsequent work later\n",
    "filepath = './data/piedmont.graphml'\n",
    "ox.save_graphml(G, filepath)\n",
    "G = ox.load_graphml(filepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if you want to work with your model in gephi, use gephi compatibility mode\n",
    "ox.save_graphml(G, filepath=filepath, gephi=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SVG file to work with in Adobe Illustrator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save street network as SVG\n",
    "fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath='./images/piedmont.svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save points of interest or building footprints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all \"amenities\" and save as a geopackage via geopandas\n",
    "gdf = ox.geometries_from_place(place, tags={'amenity':True})\n",
    "gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)\n",
    "gdf.to_file('./data/pois.gpkg', driver='GPKG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all building footprints and save as a geopackage via geopandas\n",
    "gdf = ox.geometries_from_place(place, tags={'building':True})\n",
    "gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)\n",
    "gdf.to_file('./data/building_footprints.gpkg', driver='GPKG')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save .osm XML files\n",
    "\n",
    "To save your graph to disk as a .osm formatted xml file, ensure that you created the graph with `ox.settings.all_oneway=True` for `save_graph_xml` to work properly. See docstring for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save graph to disk as .osm xml file\n",
    "ox.config(all_oneway=True, log_console=True, use_cache=True)\n",
    "G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')\n",
    "ox.save_graph_xml(G, filepath='./data/piedmont.osm')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python (ox)",
   "language": "python",
   "name": "ox"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
